package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.HibernateException;
import org.hibernate.PropertyValueException;
import org.hibernate.exception.ConstraintViolationException;

import com.paypal.sdk.exceptions.PayPalException;
import com.paypal.sdk.profiles.APIProfile;
import com.paypal.sdk.profiles.ProfileFactory;
import com.paypal.sdk.services.CallerServices;
import com.printeverywhere.delegator.CreditCardServiceDelegator;
import com.printeverywhere.delegator.CreditCardTypeServiceDelegator;
import com.printeverywhere.delegator.CurrencyServiceDelegator;
import com.printeverywhere.delegator.DepositsServiceDelegator;
import com.printeverywhere.delegator.TransactionsServiceDelegator;
import com.printeverywhere.delegator.UserServiceDelegator;
import com.printeverywhere.dto.Creditcard;
import com.printeverywhere.dto.Currency;
import com.printeverywhere.dto.Deposits;
import com.printeverywhere.dto.Transactions;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.AttributeConstants;
import com.printeverywhere.util.DateUtil;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SessionUtil;

public class MoneyManagerAction extends FrontBaseSupport implements
		ServletRequestAware {

	public MoneyManagerAction() {
		super(true, false);
	}

	/**  */
	private static final long serialVersionUID = 1L;
	private List<Creditcard> creditCardList;
	private SearchCriteria searchCriteria = new SearchCriteria();
	private HttpServletRequest request;
	private Deposits deposits;
	private Transactions transactions;
	private User user;
	private Creditcard creditcard;
	private List<Currency> currencyList = new ArrayList<Currency>();
	private double amount;
	private String nameOnCard = AdminConstants.NONE;
	private static Logger logger = Logger.getLogger(MoneyManagerAction.class);
	private String paypalEmail = "";

	@Override
	public String execute() throws Exception {
		try{
		user = SessionUtil.getUser(request);
		searchCriteria.removeAllAttribute();
		searchCriteria.setAttribute(AttributeConstants.USER, SessionUtil
				.getUser(request));
		creditCardList = CreditCardServiceDelegator.getList(searchCriteria);
		currencyList = CurrencyServiceDelegator.getList();
		}catch (ConstraintViolationException e) {
			logger.info("Constraint violated...." + e.getConstraintName() + " : " + e.getMessage());
		}catch (PropertyValueException e) {
			logger.info("Property Value Exception.... : " + e.getMessage());
		}catch (HibernateException e) {
			logger.info("Hibernate Exception.... : " + e.getMessage());
		}

		return SUCCESS;
	}

	public String updateCreditCardDeposit() {
		try {
			Date date = DateUtil.getTodaysDate();
			user = SessionUtil.getUser(request);
			deposits = DepositsServiceDelegator.get(user.getLastPaidDate());
			user.setCurrentBalance(user.getCurrentBalance() + (amount));
			user.setTotalPaid(amount);
			user.setLastPaidDate(date);
			UserServiceDelegator.updateUser(user);

			Deposits newDeposits = new Deposits();
			newDeposits.setAmount(amount);
			newDeposits.setDepositDate(date);
			newDeposits.setUserId(user.getUserId());
			newDeposits.setSource(nameOnCard);
			DepositsServiceDelegator.add(newDeposits);
			transactions = new Transactions();
			transactions.setAmount(amount);
			transactions.setBalance(user.getCurrentBalance());
			transactions.setDate(date);
			transactions.setDue(amount);
			transactions.setInvoice(AdminConstants.BLANK);
			transactions.setTitle(AdminConstants.BLANK);
			TransactionsServiceDelegator.add(transactions);
		} catch (HibernateException e) {
			logger.info("--------------->");
		}
		return SUCCESS;
	}

	public String makePaypalDeposit(){
	/*	try{
		CallerServices caller = new CallerServices();
		APIProfile profile = ProfileFactory.createSSLAPIProfile();
    	profile.setAPIUsername("sdk-seller_api1.sdk.com");
    	profile.setAPIPassword("12345678");
    	profile.setCertificateFile("Cert/sdk-seller.p12");
    	profile.setPrivateKeyPassword("password");
    	profile.setEnvironment("beta-sandbox");
    	caller.setAPIProfile(profile);

    	user = SessionUtil.getUser(request);

		}catch (PayPalException e) {
			addActionError("Unable to make payment with PayPal.");
			return INPUT;
		}*/

		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public List<Creditcard> getCreditCardList() {
		return creditCardList;
	}

	public void setCreditCardList(List<Creditcard> creditCardList) {
		this.creditCardList = creditCardList;
	}

	public Deposits getDeposits() {
		return deposits;
	}

	public void setDeposits(Deposits deposits) {
		this.deposits = deposits;
	}

	public Transactions getTransactions() {
		return transactions;
	}

	public void setTransactions(Transactions transactions) {
		this.transactions = transactions;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public String getNameOnCard() {
		return nameOnCard;
	}

	public void setNameOnCard(String nameOnCard) {
		this.nameOnCard = nameOnCard;
	}

	public Creditcard getCreditcard() {
		return creditcard;
	}

	public void setCreditcard(Creditcard creditcard) {
		this.creditcard = creditcard;
	}

	public List<Currency> getCurrencyList() {
		return currencyList;
	}

	public void setCurrencyList(List<Currency> currencyList) {
		this.currencyList = currencyList;
	}

	public String getPaypalEmail() {
		return paypalEmail;
	}

	public void setPaypalEmail(String paypalEmail) {
		this.paypalEmail = paypalEmail;
	}

}
